home *** CD-ROM | disk | FTP | other *** search
- /*
- * rfill.c
- *
- * Practical Algorithms for Image Analysis
- *
- * Copyright (c) 1997, 1998, 1999 MLMSoftwareGroup, LLC
- */
-
- /*
- * R(egion)FILL
- *
- * region growing routines
- */
- #include "xah.h"
-
- #define BLACK 0
- #define HALF_WHITE 127
- #define WHITE 255
-
-
- #define WHITE_ON_BLACK 0
- #define BLACK_ON_WHITE 1
- #undef DEBUG
-
-
-
- /*
- * mark centroid of filled region
- */
- void
- mark_centroid (double x, double y, int index, int flood_reg_index, Image * aoi)
- {
- int ix, iy;
-
- ix = ((x - (int) x) < 0.5 ? (int) x : (int) x + 1);
- iy = ((y - (int) y) < 0.5 ? (int) y : (int) y + 1);
-
- gdImageFill (ix, iy, aoi, flood_reg_index);
- setpixel (ix, iy, aoi, index);
- }
-
-
-
- /*
- * find leftmost pixel on current line which is interior to region
- */
- int
- find_left_x (int x, int y, int stop_index, Image * aoi)
- {
-
- while (getpixel (x, y, aoi) != stop_index && x > 0)
- x--;
-
- return (++x);
- }
-
-
- /*
- * fill horiz. line (from left to right)
- */
- int
- ln_fill (int x, int y, int index, double *xc, double *yc, int stop_index, Image * aoi)
- {
- int cindex;
- int ln_ctr;
-
-
- /*
- * find leftmost pixel on current line which is interior to region
- */
- x = find_left_x (x, y, stop_index, aoi);
- ln_ctr = 0;
- while ((cindex = getpixel (x, y, aoi)) != stop_index && x < aoi->width) {
- if (cindex != index) {
- //set_pix(x, y, index);
- setpixel (x, y, aoi, index);
- ln_ctr++;
- *xc += (double) x;
- *yc += (double) y;
- }
- x++;
- }
- return (ln_ctr);
- }
-
-
- /*
- * region raster growing (''fill'') routine, proceeding from seed (x,y):
- * suitable only for convex regions
- */
- unsigned int
- rfill (int x, int y, int index, double *xc, double *yc, int img_type, Image * aoi)
- {
- int ox, oy;
- unsigned int ctr, ln_ctr;
- int cindex, sindex;
- int stop_index;
- int new_ln;
- int up_full, lo_full;
-
- new_ln = 0;
- up_full = lo_full = 0;
- ctr = ln_ctr = 0;
- *xc = *yc = 0.0;
-
- ox = x;
- oy = y;
- sindex = getpixel (x, y, aoi);
- if (img_type == WHITE_ON_BLACK)
- stop_index = BLACK;
- if (img_type == BLACK_ON_WHITE)
- stop_index = WHITE;
-
-
- /*
- * fill upper portion of region, line-by-line
- */
- do {
- ln_ctr = ln_fill (x, y, index, xc, yc, stop_index, aoi);
- ctr += ln_ctr;
- #ifdef DEBUG
- printf (" (%d, %d): ln_ctr = %d, ctr = %d\n", x, y, ln_ctr, ctr);
- #endif
- if (ln_ctr == 0)
- up_full = 1;
- else {
- x = find_left_x (x, y, stop_index, aoi);
- do {
- if (getpixel (x, y - 1, aoi) == sindex)
- new_ln = 1;
- else
- x++;
- } while (((cindex = getpixel (x, y, aoi)) != stop_index) &&
- (new_ln != 1));
-
- if (new_ln == 0)
- up_full = 1;
- else {
- y--;
- new_ln = 0;
- }
- }
- } while (up_full != 1);
-
- /*
- * fill lower portion of region, line-by-line
- */
- new_ln = 0;
- x = ox;
- y = oy;
- x = find_left_x (x, y, stop_index, aoi);
-
- do {
- if (getpixel (x, y + 1, aoi) == sindex)
- new_ln = 1;
- else
- x++;
- } while (((cindex = getpixel (x, y, aoi)) != stop_index) &&
- (new_ln != 1));
-
- if (new_ln == 0)
- lo_full = 1;
- else {
- y++;
- new_ln = 0;
- }
-
-
-
- do {
- ln_ctr = ln_fill (x, y, index, xc, yc, stop_index, aoi);
- ctr += ln_ctr;
- #ifdef DEBUG
- printf (" (%d, %d): ln_ctr = %d, ctr = %d\n", x, y, ln_ctr, ctr);
- #endif
- if (ln_ctr == 0)
- lo_full = 1;
- else {
- x = find_left_x (x - 1, y, stop_index, aoi);
- do {
- if (getpixel (x, y + 1, aoi) == sindex)
- new_ln = 1;
- else
- x++;
- } while (((cindex = getpixel (x, y, aoi)) != stop_index) &&
- (new_ln != 1));
-
- if (new_ln == 0)
- lo_full = 1;
- else {
- y++;
- if (y >= aoi->height) {
- printf ("Something wrong. y value too big %d.\n", y);
- ImageOut ("imgdump.tif", aoi);
- exit (1);
- }
- new_ln = 0;
- }
- }
- } while (lo_full != 1);
-
- return (ctr);
- }
-